#pragma once
#include <osg/Vec3d>

#include "CmdHandler3.h"

bool lengthApprox2(double l1, double l2);

class Ent;

struct LessVec3f2
{
	static bool less(const osg::Vec3d& p1, const osg::Vec3d& p2)
	{
		if (!lengthApprox2(p1.x(), p2.x()))
		{
			return p1.x() < p2.x();
		}
		if (!lengthApprox2(p1.y(), p2.y()))
		{
			return p1.y() < p2.y();
		}
		if (!lengthApprox2(p1.z(), p2.z()))
		{
			return p1.z() < p2.z();
		}
		return false;
	}

	bool operator()(const osg::Vec3d& p1, const osg::Vec3d& p2) const
	{
		return less(p1, p2);
	}
};

struct CurvePoints
{
	std::shared_ptr<Ent> curve;
	std::vector<osg::Vec3d> points;
};

struct CurveEndPoint : public CurvePoints
{
	bool isFirst = false;
};

struct TwoConnectedCurves
{
	CurveEndPoint m_curve1;
	CurveEndPoint m_curve2;
};

bool constructPointCurvesMap(const std::vector<CurvePoints>& curves,
	std::map<osg::Vec3d, TwoConnectedCurves, LessVec3f2>& result, bool& open, osg::Vec3d& begin, osg::Vec3d& end);
// 这是递归函数
void connectCurve(const std::map<osg::Vec3d, TwoConnectedCurves, LessVec3f2>& ptToTwoCurveEndPointMap,
	std::set<std::shared_ptr<Ent>>& usedEnts,
	std::vector<osg::Vec3d>& result);

std::vector<osg::Vec3d> connectEnds(const std::vector<CurvePoints>& endPoints,
	std::set<std::shared_ptr<Ent>>& usedEnts);
